<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\datacache;
use think\facade\Cache;

/**
 * 视频评论回复 缓存层
 * @package app\user_center\model
 */
class VideoPlayCommentReplay{

	
	/**
     * 视频评论回复
     * @param comment_id int 视频id
     * @param cache 是否更新 缓存
     * @author shiqiang
     * @return array
     */
    public function getPlayCommentReplayList($comment_id, $cache = false){
        $cacheName = 'play_comment_replay_list_'.$comment_id;
		$list = Cache::get($cacheName);
        if(!$list || $cache){//查出评论下所有回复
            $list = model('video/VideoPlayCommentReplay') -> where('comment_id', $comment_id) -> order('support_times desc,create_time desc') -> order('id desc') -> select() -> toArray();
            if(!empty($list)){
                Cache::set($cacheName, $list);
            }
        }
       return array_out(200, 'success', $list);
    }

	/**
     * 视频回复 标准化
     * @param comment_id int 视频id
     * @param cache 是否更新 缓存
     * @author shiqiang
     * @return array
     */
    public function getDataByReplyid($user_uid, $replyid){
		$info = model('video/VideoPlayCommentReplay') -> getDataById($replyid, $cache = true);
		if($info['code'] != 200) return array_out(-1001, 'error');

		$infoObj = $info['data'];
		$result = [];
		//是否点赞
		$support = model('VideoPlayCommentReplaySupport') -> isSupport($user_uid, $infoObj->id);//是否点赞
		$isSupport = $support['code'] == 200 ? 1 : 0;
		
		$fromUserInfo = controller('user_center/Userlogic', 'logic\user') -> get_user_info($user_uid, $infoObj -> uid);//
		$replyUserInfo = controller('user_center/Userlogic', 'logic\user') -> get_user_info($user_uid, $infoObj -> r_uid);

		$result = [
			'replyId'		        => (int)$infoObj -> id,
			'replyContent'			=> (string)$infoObj -> content,
			'replySupportTimes' 	=> (int)$infoObj -> support_times,
			'replyDatetime' 		=> (int)$infoObj -> create_time,
			'replyUid'				=> (int)$infoObj -> r_uid, //如果replyUid = 0  表示 回复得评论
			'isSupport' 			=> (int)$isSupport,
			'fromUserInfo'          => $fromUserInfo['data'],
			'replyUserInfo'         => $replyUserInfo['data'],
		];

		return array_out(200, 'success', $result);
	}
}
